package com.framework.buss.bridge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.framework.buss.bridge.model.BridgeTemplateFile;
import com.framework.buss.bridge.mapper.BridgeTemplateFileMapper;
import com.framework.buss.bridge.service.IBridgeTemplateFileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BridgeTemplateFileServiceImpl implements IBridgeTemplateFileService {
    @Autowired
    private BridgeTemplateFileMapper fileMapper;

    @Override
    public void saveFile(String bridgeId, String filePath) {
        // 修改模板文件
        if (StringUtils.isBlank(filePath)) {
            return;
        }
        QueryWrapper<BridgeTemplateFile> queryWrapper = new QueryWrapper();
        queryWrapper.eq("bridge_id", bridgeId);
        BridgeTemplateFile templateFile = fileMapper.selectOne(queryWrapper);
        if (null != templateFile) {
            templateFile.setFilePath(filePath);
            fileMapper.updateById(templateFile);
        } else {
            BridgeTemplateFile file = new BridgeTemplateFile();
            file.setBridgeId(bridgeId);
            file.setFilePath(filePath);
            fileMapper.insert(file);
        }
    }

    @Override
    public String findFileId(String bridgeId) {
        QueryWrapper<BridgeTemplateFile> queryWrapper = new QueryWrapper();
        queryWrapper.eq("bridge_id", bridgeId);
        BridgeTemplateFile templateFile = fileMapper.selectOne(queryWrapper);
        if (null == templateFile) {
            throw new RuntimeException("没有配置模板");
        }
        return templateFile.getFilePath();
    }
}
